package it.blog.controller;

import java.util.StringTokenizer;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.ws.wsaddressing.W3CEndpointReference;

import it.blog.stub.*;

public class UpdateUser extends HttpServlet{


	private static final long serialVersionUID = 1L;
	private BlogPortal bp=null;


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		User user = new User();

		HttpSession session =  request.getSession();
		bp = new BlogPortalService().getPort(BlogPortal.class);
		AuthBlogPortalService us= new AuthBlogPortalService();
		
		user.setUserID(Integer.parseInt(request.getParameter("userID")));
		user.setName(request.getParameter("name"));
		user.setSurname(request.getParameter("surname"));
		user.setEmail( request.getParameter("email"));
		if(Integer.parseInt(request.getParameter("gender"))==1)
			user.setGender(true);
		else
			user.setGender(false);

		String date = request.getParameter("dateOfBirth");
		StringTokenizer st = new StringTokenizer(date,"/");
		int day = Integer.parseInt(st.nextToken());
		int month = Integer.parseInt(st.nextToken());
		int year = Integer.parseInt(st.nextToken());
		Calendar c = Calendar.getInstance();
		c.set(year, month, day);
		user.setBlogURL(request.getParameter("blogURL"));
		user.setSiteURL(request.getParameter("siteURL"));
		user.setImgURL(request.getParameter("imgURL"));
		user.setUsername(request.getParameter("username"));
		user.setPassword(request.getParameter("password"));
		user.setState(true);
		user.setRole(2);

		try {
			user.setDateOfBirth(toXMLGregorianCalendar(c));
		} catch (DatatypeConfigurationException e) {
			session.setAttribute("updateUserFail",true);
			//Rimando alla pagina cancellando la richiesta corrente
			response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/private/updateUser.jsp"));
		}

		W3CEndpointReference ref=(W3CEndpointReference)session.getAttribute("ref");
		AuthBlogPortal userAuthenticate=us.getPort(ref, AuthBlogPortal.class);
		session.removeAttribute("mess");
		if(userAuthenticate.updateUser(user)){
			session.setAttribute("user",user);
		}else{
			session.setAttribute("mess","aggiornamento utente fallito");
		}
		response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/PortalServlet"));
	}

	private static XMLGregorianCalendar toXMLGregorianCalendar(Calendar c) throws DatatypeConfigurationException {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTimeInMillis(c.getTimeInMillis());
		XMLGregorianCalendar xc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
		if(xc.getMonth()==1)
			xc.setMonth(12);
		else
			xc.setMonth(xc.getMonth()-1);
		return xc;
	}

}